package weixin.app.wx.service.rest.controller

import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RequestMapping
import wechat.base.snd.QRCodeManager
import weixin.app.WXServiceAppFactoryDemo
import wechat.base.snd.QRCode
import org.springframework.web.bind.annotation.RequestParam
import weixin.app.wx.service.rest.entities.ResultSet
import org.apache.commons.io.IOUtils
import java.io.FileInputStream
import java.util.List
import weixin.app.domain.WXCommonMessage
import weixin.app.wx.service.rest.entities.Signature
import weixin.app.wx.service.rest.util.SignatureGenerator

@RestController
class WxServiceController {

	@RequestMapping("/qrcode")
	def ResultSet<byte []> getQRCode(@RequestParam(value="scene_id") String scene_id) {
		val result = new ResultSet<byte []>()
		new QRCodeManager(WXServiceAppFactoryDemo::serviceApp) => [
			val file = genPermTicket(new QRCode(scene_id)).getTicket(scene_id)
			if (file === null) {
				result.code = "4"
				result.message = "failure"
			} else {
				result.code = "0"
				result.message = "success"
				val in = new FileInputStream(file)
				result.data = IOUtils.toByteArray(in)
			}
		]
		
		result
	}

	/*common message: text, audio, video, image */
	@RequestMapping("/message")
	def ResultSet<String> sendCommonMessage(@RequestParam(value="wcuser_ids[]") List<String> wcuser_ids,
		@RequestParam(value="content") String content) {
		
		val result = new ResultSet<String>()
		
		wcuser_ids.forEach[
			println("=============>wc user id: " + it)
		]
		
		//userid test- oQ19TwZ26yGx3Fvu4rAld1LvuWqI, wxid_qmmz6rvqtlq221
		val users = newArrayList
		users.addAll(wcuser_ids)
		WXCommonMessage::sendCommonTxtMessage(null, newArrayList(users), content)
		
		result.code = "0"
		result.message = "success"
				
		result
	
	}
	
	@RequestMapping("/signature")
	def ResultSet<Signature> getSignature(@RequestParam(value="url")String url) {
		val result = new ResultSet<Signature>()
		
		try {
			result.data = SignatureGenerator.generate(url)
			result.code = "0"
			result.message = "success"
		} catch (Exception e) {
			result.code = "4"
			result.message = "failure"
		}
		
		
		result
	}

	@RequestMapping("/templatemessage")
	def ResultSet<String> sendTemplateMessage() {
		val result = new ResultSet<String>()
		
		
		result
	}

}
/*usage: <img src="data:image/jpeg;base64, {Imagedata}"> */ 